﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace BerkelyAlgorithem2
{
    class tcpServer
    {
        public TcpListener tcpListener;
        IPAddress ipAddress;
        public Neighbour ident;
        TcpClient cc;
        System.Windows.Forms.Timer t;

        public tcpServer(IPAddress ipAddress, int port)
        {
            //t = new System.Windows.Forms.Timer();
            //t.Tick += new EventHandler(OnTimedEvent);
            //t.Interval = 5000;

            try
            {

                // IPAddress ipAddress = IPAddress.Parse(ip); //use local m/c IP address, and use the same in the client
                /* Initializes the Listener */
                this.ipAddress = ipAddress;
                tcpListener = new TcpListener(ipAddress, port);
                /* Start Listeneting at the specified port */
                //tcpListener.Server.ReceiveTimeout = 5000;

                tcpListener.Start();
                ////Console.WriteLine("The server is running at port 8001...");
                ////Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint);
                ////Console.WriteLine("Waiting for a connection.....");
            }
            catch (Exception e)
            {
                //Console.WriteLine("Error in TCPServer on port: " + port + "\n" + e.StackTrace);
            }
        }

        public tcpServer(IPAddress ipAddress, int port, Neighbour n)
        {
            this.ident = n;
            try
            {

                // IPAddress ipAddress = IPAddress.Parse(ip); //use local m/c IP address, and use the same in the client
                /* Initializes the Listener */
                this.ipAddress = ipAddress;
                tcpListener = new TcpListener(ipAddress, port);
                /* Start Listeneting at the specified port */
                tcpListener.Start();
                ////Console.WriteLine("The server is running at port 8001...");
                ////Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint);
                ////Console.WriteLine("Waiting for a connection.....");    

            }
            catch (Exception e)
            {
                //Console.WriteLine("Error in TCPServer on port: " + port + "\n" + e.StackTrace);
            }
        }


        public byte[] listenAndGetMsg()
        {
            byte[] msg = null;
            System.Threading.Thread newThread;
            newThread = new System.Threading.Thread(() => { msg = getMsg(); });
            newThread.IsBackground = true;
            newThread.Start();
            newThread.Join();

            return msg;
        }

        public void exit()
        {
            tcpListener.Stop();
        }

        public byte[] getMsg()
        {
            //start timeout
            //t.Start();
            
            //Console.WriteLine("in tcpServer getting msg from " + ipAddress + " ");

            byte[] word = new byte[100];
            if (cc == null)
                cc = tcpListener.AcceptTcpClient();


            NetworkStream stream = cc.GetStream();
            int k = stream.Read(word, 0, word.Length);
            
            //all ok
            //t.Stop();
            //t.Interval = 5000;

            byte[] ret = new byte[k];

            for (int i = 0; i < k; i++)
                ret[i] = word[i];

            //Console.WriteLine("in tcpServer got " + utils.ByteToStrArray(word));
            //stream.Dispose();
            return word;
        }

        //private void OnTimedEvent(object sender, EventArgs e)
        //{
        //    t.Stop();
        //    t.Interval = 5000;
        //    NetworkStream stream = cc.GetStream();
        //    stream.Close();
        //    throw new NotFiniteNumberException();
        //}
    }
}
